<template>
  <div class="login-wrapper">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" class="login-form">
      <div class="left">
        <h3>后台管理系统</h3>
      </div>
      <div class="right">
        <h3>登陆</h3>
        <el-form-item prop="username">
          <el-input style="width:50%;" type="text" v-model="ruleForm.username" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input style="width:50%;" type="password" v-model="ruleForm.pwd" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import { LoginApi } from "@/request/api";

export default {
  data() {
    return {
      ruleForm: {},
      rules: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" },
          {
            min: 5,
            max: 50,
            message: "字符长度最小为 5 个字符",
            trigger: "blur",
          },
        ],
        pwd: [{ required: true, message: "密码不能为空", trigger: "blur" },
        {
            min: 5,
            max: 50,
            message: "字符长度最小为 5 个字符",
            trigger: "blur",
          },],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs["ruleForm"].validate(async (vali) => {
        console.log("vali:" + vali);
        if (vali) {
          const result = await LoginApi({
            UserName: this.ruleForm.username,
            Password: this.ruleForm.pwd,
          });
          if (result.StatusCode == 200) {
            // debugger
            localStorage.setItem("token", result.Data.Ticket);
            this.$router.push("/statistical");
          }
          console.log(result);
          //存储管理员账户
          const user = result.Data.UserName
          localStorage.setItem('roles',user)
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #324057;
  .login-form {
    width: 60%;
    text-align: center;
    display: flex;
    box-sizing: border-box;
    background-color: #fff;
    box-shadow: 10px 10px 8px;
    padding: 25px;
    .left {
      flex: 1;
      border-right: 1px solid black;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      h3 {
        padding-bottom: 15px;
        font-size: 24px;
        color: black;
        margin-top: 20px;
      }
    }
    .right {
      h3 {
        margin-bottom: 20px;
        margin-top: 20px;
      }
      flex: 3;
      .login-btn,
      input {
        width: 50%;
      }
    }
  }
}
.el-form-item__content{
  text-align: center;
    justify-content: center;
}
</style>
